package com.it.common.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.common.domain.dos.TagDO;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;


public interface TagMapper extends BaseMapper<TagDO> {


    /**
     * 插入操作
     * @param tags
     */
    void insertBatch(List<TagDO> tags);


    /**
     *分页查询
     * @return
     */
    default Page<TagDO> selectPageList(long current, long size, String name, LocalDate startDate, LocalDate endDate){
        Page<TagDO> page = new Page<>(current,size);
        LambdaQueryWrapper<TagDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(name),TagDO::getName, name)
                .ge(Objects.nonNull(startDate),TagDO::getCreateTime,startDate)
                .le(Objects.nonNull(endDate),TagDO::getCreateTime,endDate)
                .orderByDesc(TagDO::getCreateTime);

        return selectPage(page, wrapper);
    }

    Boolean deleteTag(Long id);

    List<TagDO> selectByKey(String key);

    List<TagDO> findAll();

    /**
     * 根据标签 ID 批量查询
     * @param tagIds
     * @return {@link List }<{@link TagDO }>
     */
    List<TagDO> selectByIds(List<Long> tagIds);


    /**
     * 查询标签总数
     * @return {@link Long }
     */
    Long selectTagTotal();

    default List<TagDO> selectByLimit(Long limit) {
        return selectList(Wrappers.<TagDO>lambdaQuery()
                .orderByDesc(TagDO::getArticlesTotal) // 按文章总数倒序排列
                .last(String.format("LIMIT %d", limit))); // 查询指定数量
    }

}
